home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte16 / crtclrsp.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  8KB  |  230 lines

  1.  
  2. #include <windows.h>  
  3. #include "CrtClrSp.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    switch( uMsg )
  110.    {
  111.       case WM_COMMAND :
  112.               switch( LOWORD( wParam ) )
  113.               {
  114.                  case IDM_TEST :
  115.                         {
  116.                            LOGCOLORSPACE ColorSpace;
  117.                            HCOLORSPACE   hColorSpace;
  118.                            HBRUSH        hBrush, hOldBrush;
  119.                            HDC hDC = GetDC( hWnd );
  120.  
  121.                            // Fill LOGCOLORSPACE structure with 
  122.                            // appropriate values.
  123.                            //..................................
  124.                            ColorSpace.lcsSignature= (DWORD)0x50534F43;
  125.                            ColorSpace.lcsVersion  = 0x00000400;
  126.                            ColorSpace.lcsSize     = sizeof( ColorSpace );
  127.                            ColorSpace.lcsCSType   = LCS_DEVICE_RGB;
  128.                            ColorSpace.lcsIntent   = LCS_GM_BUSINESS;
  129.  
  130.                            ColorSpace.lcsGammaRed   = 0x00010000;
  131.                            ColorSpace.lcsGammaGreen = 0x00010000;
  132.                            ColorSpace.lcsGammaBlue  = 0x00010000;
  133.  
  134.                            ColorSpace.lcsEndpoints.ciexyzRed.ciexyzX = 0x000051FB;
  135.                            ColorSpace.lcsEndpoints.ciexyzRed.ciexyzY = 0x000051EB;
  136.                            ColorSpace.lcsEndpoints.ciexyzRed.ciexyzZ = 0x00002B85;
  137.  
  138.                            ColorSpace.lcsEndpoints.ciexyzGreen.ciexyzX = 0x0000186F;
  139.                            ColorSpace.lcsEndpoints.ciexyzGreen.ciexyzY = 0x0000552D;
  140.                            ColorSpace.lcsEndpoints.ciexyzGreen.ciexyzZ = 0x0000BA23;
  141.  
  142.                            ColorSpace.lcsEndpoints.ciexyzBlue.ciexyzX = 0x00011693;
  143.                            ColorSpace.lcsEndpoints.ciexyzBlue.ciexyzY = 0x00003D46;
  144.                            ColorSpace.lcsEndpoints.ciexyzBlue.ciexyzZ = 0x00001DED;
  145.  
  146.                            ColorSpace.lcsFilename[0] = 0;
  147.  
  148.                            // Create the color space and blue brush.
  149.                            //.......................................
  150.                            hColorSpace = CreateColorSpace( &ColorSpace );
  151.                            hBrush      = CreateSolidBrush( RGB( 0, 0, 255 ) );
  152.  
  153.                            // Select the blue brush into the device context.
  154.                            //...............................................
  155.                            hOldBrush = SelectObject( hDC, hBrush );
  156.  
  157.                            // Draw a blue rectangle.
  158.                            //.......................
  159.                            Rectangle( hDC, 10, 10, 50, 50 );
  160.  
  161.                            // Enable image color matching and 
  162.                            // set the color space.
  163.                            //................................
  164.                            SetICMMode( hDC, ICM_ON );
  165.                            SetColorSpace( hDC, hColorSpace );
  166.  
  167.                            // Draw another "blue" rectangle that 
  168.                            // shows up as a red rectangle because
  169.                            // of the color space.
  170.                            //....................................
  171.                            Rectangle( hDC, 70, 10, 110, 50 );
  172.  
  173.                            // Clean up.
  174.                            //..........
  175.                            SetICMMode( hDC, ICM_OFF );
  176.  
  177.                            SelectObject( hDC, hOldBrush );
  178.  
  179.                            DeleteColorSpace( hColorSpace );
  180.                            DeleteObject( hBrush );
  181.  
  182.                            ReleaseDC( hWnd, hDC );
  183.                         }
  184.                         break;
  185.  
  186.                  case IDM_ABOUT :
  187.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  188.                         break;
  189.  
  190.                  case IDM_EXIT :
  191.                         DestroyWindow( hWnd );
  192.                         break;
  193.               }
  194.               break;
  195.       
  196.       case WM_DESTROY :
  197.               PostQuitMessage(0);
  198.               break;
  199.  
  200.       default :
  201.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  202.    }
  203.  
  204.    return( 0L );
  205. }
  206.  
  207.  
  208. LRESULT CALLBACK About( HWND hDlg,           
  209.                         UINT message,        
  210.                         WPARAM wParam,       
  211.                         LPARAM lParam)
  212. {
  213.    switch (message) 
  214.    {
  215.        case WM_INITDIALOG: 
  216.                return (TRUE);
  217.  
  218.        case WM_COMMAND:                              
  219.                if (   LOWORD(wParam) == IDOK         
  220.                    || LOWORD(wParam) == IDCANCEL)    
  221.                {
  222.                        EndDialog(hDlg, TRUE);        
  223.                        return (TRUE);
  224.                }
  225.                break;
  226.    }
  227.  
  228.    return (FALSE); 
  229. }
  230.